package com.ewanghuiju.app.ui.resale.fragment;

import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RootFragment;
import com.ewanghuiju.app.base.contract.resale.ResaleHomeContract;
import com.ewanghuiju.app.model.bean.response.ResaleShopResponBean;
import com.ewanghuiju.app.model.bean.response.WaterGoodsClassifyBean;
import com.ewanghuiju.app.model.bean.response.WaterShopResponBean;
import com.ewanghuiju.app.presenter.resale.ResaleHomePresenter;
import com.ewanghuiju.app.ui.resale.adapter.ResaleShopListAdapter;
import com.ewanghuiju.app.ui.watermail.activity.WaterShopDetailsActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 转售商品列表
 * @time 2020/2/28
 */
public class ResaleHomeFragment extends RootFragment<ResaleHomePresenter> implements ResaleHomeContract.View {
    @BindView(R.id.view_main)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.layout_screen_main)
    LinearLayout layoutScreenMain;
    private int mType;

    private ResaleShopListAdapter resaleShopListAdapter;
    private int currentPage = 1;

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_resale_home;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        layoutScreenMain.setBackground(null);
        mType = getArguments().getInt(Constants.WATER_TYPE);
        intWaterShopList();
    }

    /**
     * 热门团购 加载商品列表
     */
    private void intWaterShopList() {
        recyclerView.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        resaleShopListAdapter = new ResaleShopListAdapter(R.layout.item_resale_shop_list);
        resaleShopListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WaterShopResponBean waterShopResponBean = (WaterShopResponBean) adapter.getItem(position);
                new StartActivityUtil(activity, WaterShopDetailsActivity.class).putExtra(Constants.WATER_SHOP_ID,
                        waterShopResponBean.getId())
                        .startActivity(true);
            }
        });
        recyclerView.setAdapter(resaleShopListAdapter);
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(activity);
                currentPage++;
                mPresenter.getData(currentPage, mType + "");
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(activity);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getData(currentPage, mType + "");
            }
        });
        //首次进入显示
        LoadingDialogUtils.show(activity);
        currentPage = 1;
        mPresenter.getData(currentPage, mType + "");
    }

    @Override
    public void initImmersionBar() {
//        super.initImmersionBar();
    }

    @Override
    public void showContent(List<ResaleShopResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE_TWO) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            resaleShopListAdapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE_TWO) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            resaleShopListAdapter.addData(list);
        }

        if (resaleShopListAdapter.getData().size() > 0) {
            stateMain();
        } else {
            stateEmpty();
        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
        if (resaleShopListAdapter.getData().size() == 0) {
            super.stateError();
        }
    }

    @Override
    public void showJfgoodscate(List<WaterGoodsClassifyBean> list) {

    }


    @Override
    public void stateError() {
        super.stateError();
    }

}